﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace AISFMIOperator
{
    class Specialty
    {
        public Specialty()
        { }

        public int SpecialtyID
        { get; set; }

        public string SpecialtyName
        { get; set; }

        public string Description
        { get; set; }

        public void Save()
        {
            SqlCommand command = new SqlCommand("CreateSpecialty");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@specialty_name", SqlDbType.VarChar, 255).Value = SpecialtyName;
            command.Parameters.Add("@description", SqlDbType.VarChar, 255).Value = Description;
            command.Parameters.Add("@specialty_id", SqlDbType.Int).Direction = ParameterDirection.Output;
            SQLDBLayer.ExecSPNonQuery(command);
            this.SpecialtyID = (int)command.Parameters["@specialty_id"].Value;
        }

        public void Update()
        {
            SqlCommand command = new SqlCommand("UpdateSpecialty");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@specialty_name", SqlDbType.VarChar, 255).Value = SpecialtyName;
            command.Parameters.Add("@description", SqlDbType.VarChar, 255).Value = Description;
            command.Parameters.Add("@specialty_id", SqlDbType.Int).Value = SpecialtyID;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public void Delete()
        {
            SqlCommand command = new SqlCommand("DeleteSpecialty");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@specialty_id", SqlDbType.Int).Value = SpecialtyID;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public static List<Specialty> GetAllSpecialties()
        {
            DataTable table = new DataTable();
            List<Specialty> specialties = new List<Specialty>();
            SqlCommand command = new SqlCommand("GetSpecialties");
            command.CommandType = CommandType.StoredProcedure;

            table = SQLDBLayer.ExecSPTable(command);

            foreach (DataRow row in table.Rows)
            {
                Specialty specialty = new Specialty();
                specialty.SpecialtyID = (int)row["specialty_id"];
                specialty.SpecialtyName = (string)row["specialty_name"];
                specialty.Description = (string)row["description"];

                specialties.Add(specialty);
            }

            return specialties;
        }
    }
}
